package nc.ui.so.m30.billui.action;

import java.awt.event.ActionEvent;
import java.util.List;

import nc.itf.pubapp.pub.exception.IResumeException;
import nc.ui.pubapp.uif2app.AppUiState;
import nc.ui.pubapp.uif2app.actions.pflow.ApproveScriptAction;
import nc.ui.scmpub.util.ResumeExceptionUIProcessUtils;
import nc.vo.so.m30.entity.SaleOrderVO;
import nc.vo.so.pub.enumeration.BillStatus;

@SuppressWarnings("restriction")
public class SaleOrderApproveAction extends ApproveScriptAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void doAction(ActionEvent e) throws Exception {
		SaleOrderVO billvo = (SaleOrderVO)getModel().getSelectedData();
		String ts = billvo.getParentVO().getTs().toString();
		String oldpk = billvo.getParentVO().getCsaleorderid();
		Integer flag = billvo.getParentVO().getFstatusflag();
		super.doAction(e);
		List<?> list = getModel().getData();
		int size = list.size() ;
		for (int i = list.size() - 1; i >= 0; i--) {
			SaleOrderVO bill = ((SaleOrderVO) list.get(i));
			String pk = bill.getParentVO().getCsaleorderid();
			String nts = bill.getParentVO().getTs().toString();
			if (oldpk.equals(pk) && !ts.equals(nts)&&(BillStatus.I_AUDITING==flag||bill.getParentVO().getFstatusflag() == BillStatus.I_AUDITING)) {
				list.remove(i);
			}
		}
		if(size!= list.size()){
		  getModel().initModel(list.toArray());
		}
	}

	@Override
	protected boolean isActionEnable() {

		Object[] seldatas = this.model.getSelectedOperaDatas();

		if (this.model.getAppUiState() == AppUiState.NOT_EDIT
				&& null != seldatas && seldatas.length > 1) {
			return true;
		}
		Object selectedData = this.model.getSelectedData();
		Integer status = null;
		if (null != selectedData && selectedData instanceof SaleOrderVO) {
			SaleOrderVO selorder = (SaleOrderVO) selectedData;
			status = selorder.getParentVO().getFstatusflag();
		}
		boolean isEnable = this.model.getAppUiState() == AppUiState.NOT_EDIT
				&& selectedData != null
				&& (BillStatus.FREE.equalsValue(status) || BillStatus.AUDITING
						.equalsValue(status));

		return isEnable;

	}

	@Override
	protected boolean isResume(IResumeException resumeInfo) {
		return ResumeExceptionUIProcessUtils.isResume(resumeInfo,
				getFlowContext());
	}
}
